package com.ruoyi.web.controller.system;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.RecZmPaperInsideManagement;
import com.ruoyi.system.domain.RecordArchivesBook;
import com.ruoyi.system.service.IRecZmPaperInsideManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 专门档案 卷内管理Controller
 *
 * @author yzf
 * @date 2022-06-03
 */
@RestController
@RequestMapping("/system/ZmInsideManagement")
public class RecZmPaperInsideManagementController extends BaseController {
    @Autowired
    private IRecZmPaperInsideManagementService recZmPaperInsideManagementService;

    /**
     * 查询专门档案 卷内管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:ZmInsideManagement:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecZmPaperInsideManagement recZmPaperInsideManagement) {
        startPage();
        List<RecZmPaperInsideManagement> list = recZmPaperInsideManagementService.selectRecZmPaperInsideManagementList(recZmPaperInsideManagement);
        return getDataTable(list);
    }

    /**
     * 查询专门档案 馆藏档案 列表
     */
    @PreAuthorize("@ss.hasPermi('system:ZmInsideManagement:list')")
    @GetMapping("/listbook")
    public TableDataInfo listbook(RecZmPaperInsideManagement recZmPaperInsideManagement) {
        startPage();
        List<RecordArchivesBook> list = recZmPaperInsideManagementService.selectlistBook(recZmPaperInsideManagement);
        return getDataTable(list);
    }

    /**
     * 查询专门档案  馆藏档案 导出
     */
    @PreAuthorize("@ss.hasPermi('system:ZmInsideManagement:list')")
    @GetMapping("/listbookExport")
    public void listbookExport(RecZmPaperInsideManagement recZmPaperInsideManagement, HttpServletResponse response) {
        List<RecordArchivesBook> list = recZmPaperInsideManagementService.selectlistBook(recZmPaperInsideManagement);
        ExcelUtil<RecordArchivesBook> util = new ExcelUtil<RecordArchivesBook>(RecordArchivesBook.class);
        util.exportExcel(response, list, "专门档案 卷内管理数据");
    }

    /****
     *根据题名查询案卷
     * @param recZmPaperInsideManagement
     * @return
     */
    @GetMapping("/listByName")
    public AjaxResult listByName(RecZmPaperInsideManagement recZmPaperInsideManagement) {
        List<Map<String, Object>> list = recZmPaperInsideManagementService.selectlistByName(recZmPaperInsideManagement);
        return AjaxResult.success(list);
    }

    /**
     * 导出专门档案 卷内管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:ZmInsideManagement:export')")
    @Log(title = "专门档案 卷内管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecZmPaperInsideManagement recZmPaperInsideManagement) {
        List<RecZmPaperInsideManagement> list = recZmPaperInsideManagementService.selectRecZmPaperInsideManagementList(recZmPaperInsideManagement);
        ExcelUtil<RecZmPaperInsideManagement> util = new ExcelUtil<RecZmPaperInsideManagement>(RecZmPaperInsideManagement.class);
        util.exportExcel(response, list, "专门档案 卷内管理数据");
    }

    /**
     * 获取专门档案 卷内管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:ZmInsideManagement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(recZmPaperInsideManagementService.selectRecZmPaperInsideManagementById(id));
    }

    /**
     * 新增专门档案 卷内管理
     */
    @PreAuthorize("@ss.hasPermi('system:ZmInsideManagement:add')")
    @Log(title = "专门档案 卷内管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecZmPaperInsideManagement recZmPaperInsideManagement) {
        return toAjax(recZmPaperInsideManagementService.insertRecZmPaperInsideManagement(recZmPaperInsideManagement));
    }

    /**
     * 修改专门档案 卷内管理file_date
     */
    @PreAuthorize("@ss.hasPermi('system:ZmInsideManagement:edit')")
    @Log(title = "专门档案 卷内管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecZmPaperInsideManagement recZmPaperInsideManagement) {
        return toAjax(recZmPaperInsideManagementService.updateRecZmPaperInsideManagement(recZmPaperInsideManagement));
    }

    /**
     * 修改专门档案 批量文件上传
     */
    @PreAuthorize("@ss.hasPermi('system:ZmInsideManagement:multFile')")
    @Log(title = "专门档案 卷内管理 批量文件上传", businessType = BusinessType.UPDATE)
    @PostMapping("/multFile")
    public AjaxResult multFile(@RequestBody RecZmPaperInsideManagement recZmPaperInsideManagement) {
        int i = recZmPaperInsideManagementService.upLoadMultFile(recZmPaperInsideManagement);
        if(i > 0){
            return AjaxResult.success("成功上传" + i + "条数据");
        }else {
            return AjaxResult.error("上传失败，请检查挂载序号！");
        }
    }

    /**
     * 删除专门档案 卷内管理
     */
    @PreAuthorize("@ss.hasPermi('system:ZmInsideManagement:remove')")
    @Log(title = "专门档案 卷内管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(recZmPaperInsideManagementService.deleteRecZmPaperInsideManagementByIds(ids));
    }

    /**
     * 案卷归档申请
     */
    @Log(title = "案卷归档申请", businessType = BusinessType.UPDATE)
    @PutMapping("/apply/{ids}")
    public AjaxResult updateArchivesApply(@PathVariable String[] ids) {
        return toAjax(recZmPaperInsideManagementService.updateArchivesApplyByIds(ids));
    }

    /**
     * 案卷归档通过
     */
    @Log(title = "案卷归档通过", businessType = BusinessType.UPDATE)
    @PutMapping("/approve/{ids}")
    public AjaxResult updateArchivesApprove(@PathVariable String[] ids) {
        return toAjax(recZmPaperInsideManagementService.updateArchivesApprove(ids));
    }

    /**
     * 案卷归档拒绝
     */
    @Log(title = "案卷归档拒绝", businessType = BusinessType.UPDATE)
    @PutMapping("/reject/{ids}")
    public AjaxResult updateArchivesReject(@PathVariable String[] ids) {
        return toAjax(recZmPaperInsideManagementService.updateArchivesReject(ids));
    }

    /**
     * 文件导入
     */
    @Log(title = "文件导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:ZmInsideManagement:importData')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<RecZmPaperInsideManagement> util = new ExcelUtil<RecZmPaperInsideManagement>(RecZmPaperInsideManagement.class);
        List<RecZmPaperInsideManagement> fileList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = recZmPaperInsideManagementService.importFile(fileList, operName);
        return AjaxResult.success(message);
    }

    /**
     * 导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<RecZmPaperInsideManagement> util = new ExcelUtil<RecZmPaperInsideManagement>(RecZmPaperInsideManagement.class);
        util.importTemplateExcel(response, "项目工程数据");
    }

    /**
     * 导出文件管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:export')")
    @Log(title = "案卷包导出", businessType = BusinessType.EXPORT)
    @PostMapping("/anJuanBaoExport/{archivesId}")
    public void anJuanBaoExport(@PathVariable("archivesId") String archivesId, HttpServletRequest request, HttpServletResponse response) {
        //生成文件并反回
        File resfile = recZmPaperInsideManagementService.selectRecordFileExport(archivesId);
        if (ObjectUtil.isEmpty(resfile)) {
            return;
        }
        try {
            String downloadName = "案卷.zip";
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, downloadName);
            // FileUtils.writeBytes(resfile.getAbsolutePath(), response.getOutputStream());
            FileUtil.writeToStream(resfile, response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            resfile.delete();
        }

    }


}
